package org.grow.loginto.controller;

import kotlin.UnsafeVariance;
import lombok.extern.slf4j.Slf4j;
import org.grow.loginto.dao.UserInfoDao;
import org.grow.loginto.entity.Leave;
import org.grow.loginto.entity.UserInfo;
import org.grow.loginto.exception.JulyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.filter.CharacterEncodingFilter;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: xwg
 * @CreateDate: 21-6-2
 */
@Slf4j
@RestController
public class SomeController {
    @Autowired
    private UserInfoDao userInfoDao;
    @PostMapping("/userInfo")
    public UserInfo insert(@RequestBody UserInfo userInfo){
        if (userInfo.getUserId()!=0) throw new JulyException("id wrong");
         return userInfoDao.save(userInfo);

    }

    @PostMapping("/applyLeave")
    public Boolean apply(@RequestBody Leave leave){
        System.out.println(leave);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null){
            throw new JulyException("auth为空,请重新登录",920);
        }
        if (auth.getPrincipal() instanceof UserDetails){

            UserDetails principal = (UserDetails) auth.getPrincipal();
            System.out.println(principal);;
            log.warn(principal.getUsername()+"请假"+leave.getDays()+"天");
            return  true;
        }else {
            throw new JulyException("auth里user不为userDetails",921);
        }


    }
    @Value("${server.port}")
    private String port;
    @GetMapping("/forSome")
    public Map<String,Object> forSome(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("port",port);
        map.put("time", LocalDateTime.now());
        return map;
    }
//    CharacterEncodingFilter
//    characterEncodingFilter


//
}
